package com.itc.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huawei.shade.org.joda.time.LocalDateTime;
import com.itc.common.core.domain.R;
import com.itc.dispatch.domain.SysFlowType;
import com.itc.dispatch.domain.dto.SysFlowTypeDto;
import com.itc.dispatch.domain.dto.SysFlowTypePageDto;
import com.itc.dispatch.domain.vo.SysFlowTypeVo;
import com.itc.dispatch.service.SysFlowTypeService;
import com.itc.dispatch.mapper.SysFlowTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 智少
* @description 针对表【sys_flow_type】的数据库操作Service实现
* @createDate 2024-08-22 16:02:48
*/
@Service
public class SysFlowTypeServiceImpl extends ServiceImpl<SysFlowTypeMapper, SysFlowType>
    implements SysFlowTypeService{

    @Override
    @Transactional
    public R add(SysFlowTypeDto dto) {
        //添加的是父级
        if (dto.getParentId() == null) {
            LambdaQueryWrapper<SysFlowType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysFlowType::getFlowName,dto.getFlowName());
            Long aLong = this.baseMapper.selectCount(queryWrapper);
            if (aLong > 0){
                return R.fail("新增失败，该父级已包含");
            }
            SysFlowType sysFlowType = new SysFlowType();
            BeanUtils.copyProperties(dto,sysFlowType);
            sysFlowType.setDelete(0);
            sysFlowType.setCreateDate(LocalDate.now().toString());
            this.baseMapper.insert(sysFlowType);
            return R.ok("新增成功");
        }
        //获取父级元素
        LambdaQueryWrapper<SysFlowType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFlowType::getId,dto.getParentId());
        queryWrapper.last("limit 1");
        SysFlowType sysFlowTypeParent = this.baseMapper.selectOne(queryWrapper);
        if (sysFlowTypeParent == null){
            return R.fail("新增失败，父级不存在");
        }
        //添加子级
        LambdaQueryWrapper<SysFlowType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysFlowType::getParentId,dto.getParentId());
        List<SysFlowType> sysFlowTypes = this.baseMapper.selectList(wrapper);
        boolean bool = sysFlowTypes.stream().anyMatch(one -> dto.getFlowName().equals(one.getFlowName()));
        if (bool){
            return R.fail("新增失败，该子级名称已重复");
        }
        SysFlowType sysFlowTypechildren = new SysFlowType();
        BeanUtils.copyProperties(dto,sysFlowTypechildren);
        sysFlowTypechildren.setDelete(0);
        sysFlowTypechildren.setParentId(sysFlowTypeParent.getId());
        sysFlowTypechildren.setCreateDate(LocalDate.now().toString());
        this.baseMapper.insert(sysFlowTypechildren);
        return R.ok("新增成功");
    }

    @Override
    @Transactional
    public R delete(SysFlowTypeDto sysFlowTypeDto) {
        SysFlowType sysFlowType = this.baseMapper.selectById(sysFlowTypeDto.getId());
        if (sysFlowType == null){
            return R.fail("数据不存在");
        }
      //删除父级
        if (sysFlowTypeDto.getParentId() == null){
            sysFlowType.setDelete(1);
            this.baseMapper.updateById(sysFlowType);
            //清空子级
            this.update(new LambdaUpdateWrapper<SysFlowType>().eq(SysFlowType::getParentId,sysFlowTypeDto.getId()).set(SysFlowType::getDelete,1));
            return R.ok("删除成功");
        }
        //删除子级
        sysFlowType.setDelete(1);
        this.baseMapper.updateById(sysFlowType);
        return R.ok("删除成功");
    }
    @Override
    public R updateOne(SysFlowType sysFlowType) {
        SysFlowType sysFlowTypeOne = this.baseMapper.selectById(sysFlowType.getId());
        if (sysFlowTypeOne == null){
            return R.ok("数据不存在");
        }
        //更新父级
        if (sysFlowType.getParentId() == null){
            if (!Objects.equals(sysFlowTypeOne.getFlowName(), sysFlowType.getFlowName())){
                LambdaQueryWrapper<SysFlowType> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysFlowType::getFlowName,sysFlowType.getFlowName());
                Long aLongParent = this.baseMapper.selectCount(wrapper);
                if (aLongParent > 0){
                    return R.fail("更新失败，父类名称重复");
                }
            }
            this.baseMapper.updateById(sysFlowType);
            return R.ok("更新成功");
        }
        //更新子级
        if (!Objects.equals(sysFlowTypeOne.getFlowName(), sysFlowType.getFlowName()) && !Objects.equals(sysFlowTypeOne.getParentId(), sysFlowType.getParentId())){
            SysFlowType sysFlowTypeParent = this.baseMapper.selectById(sysFlowType.getParentId());
            if (sysFlowTypeParent == null){
                return R.fail("父级不存在");
            }
            LambdaQueryWrapper<SysFlowType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysFlowType::getParentId,sysFlowTypeParent.getId());
            List<SysFlowType> sysFlowTypeChildren = this.baseMapper.selectList(wrapper);
            boolean bool = sysFlowTypeChildren.stream().anyMatch(one -> sysFlowType.getFlowName().equals(one.getFlowName()));
            if (bool){
                return R.fail("该父级下，流程菜单名称已存在");
            }
        }
        this.baseMapper.updateById(sysFlowType);
        return R.ok("更新成功");
    }

    @Override
    public R<Page<SysFlowTypeVo>> getPage(Page page, SysFlowTypePageDto pageDto) {
        //查询全部父亲分页消息
        LambdaQueryWrapper<SysFlowType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pageDto.getFlowName()!= null,SysFlowType::getFlowName,pageDto.getFlowName());
        queryWrapper.eq(SysFlowType::getDelete,0);
        queryWrapper.isNull(SysFlowType::getParentId);
        Page pageResult = this.baseMapper.selectPage(page, queryWrapper);
        List records = pageResult.getRecords();
        Page<SysFlowTypeVo> sysFlowTypeVoPage = new Page<>();
        sysFlowTypeVoPage.setSize(pageDto.getSize());
        sysFlowTypeVoPage.setCurrent(pageDto.getCurrent());
        sysFlowTypeVoPage.setTotal(records.size());
        ArrayList<SysFlowTypeVo> sysFlowTypeVos = new ArrayList<>();
        for (Object record : records) {
            SysFlowTypeVo sysFlowTypeVo = new SysFlowTypeVo();
            BeanUtils.copyProperties(record,sysFlowTypeVo);
            LambdaQueryWrapper<SysFlowType> wrapper = new LambdaQueryWrapper<>();
            SysFlowType parent= (SysFlowType) record;
            wrapper.eq(SysFlowType::getParentId,parent.getId());
            wrapper.eq(SysFlowType::getDelete,0);
            List<SysFlowType> sysFlowChildens = this.baseMapper.selectList(wrapper);
            sysFlowTypeVo.setFlowChildens(sysFlowChildens);
            sysFlowTypeVos.add(sysFlowTypeVo);
        }
        sysFlowTypeVoPage.setRecords(sysFlowTypeVos);
        return R.ok(sysFlowTypeVoPage);
    }

    @Override
    public R<List<SysFlowTypeVo>> getList(SysFlowType sysFlowType) {
        //获取全部父级
        LambdaQueryWrapper<SysFlowType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(sysFlowType.getFlowName() != null,SysFlowType::getFlowName,sysFlowType.getFlowName());
        queryWrapper.eq(SysFlowType::getDelete,0);
        queryWrapper.isNull(SysFlowType::getParentId);
        List<SysFlowType> sysFlowTypesParent = this.baseMapper.selectList(queryWrapper);
        ArrayList<SysFlowTypeVo> sysFlowTypeVoList = new ArrayList<>();
        //获取全部子级
        for (SysFlowType flowTypeParent : sysFlowTypesParent) {
            SysFlowTypeVo sysFlowTypeVo = new SysFlowTypeVo();
            BeanUtils.copyProperties(flowTypeParent,sysFlowTypeVo);
            LambdaQueryWrapper<SysFlowType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysFlowType::getParentId,flowTypeParent.getId());
            List<SysFlowType> sysFlowTypeChildrens = this.baseMapper.selectList(wrapper);
            sysFlowTypeVo.setFlowChildens(sysFlowTypeChildrens);
            sysFlowTypeVoList.add(sysFlowTypeVo);
        }
        return R.ok(sysFlowTypeVoList);
    }
}




